/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.usc.cs576.lib.xml;

import edu.usc.cs576.lib.Action;
import edu.usc.cs576.lib.mediaObject.Audio;
import edu.usc.cs576.lib.mediaObject.Image;
import edu.usc.cs576.lib.mediaObject.KeyFrame;
import edu.usc.cs576.lib.mediaObject.Track;
import edu.usc.cs576.lib.mediaObject.Video;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;

/**
 *
 * @author haoxu
 */
public class XMLWriter {

    String filename;
    String absolutePath;
    FileOutputStream fos;

    public XMLWriter(String filename, String absolutePath) {
        this.filename = filename;
        this.absolutePath = absolutePath;
    }

    public void initial() {
        try {
            if (!absolutePath.endsWith(".my576")) {
                absolutePath = new StringBuilder(absolutePath + ".my576").toString();
            }

            File output = new File(absolutePath);
            if (output.exists()) {
                output.delete();
                output.createNewFile();
            }

            fos = new FileOutputStream(output);

            StringBuilder sb = new StringBuilder("");
            sb.append("<MediaFile>\n");
            fos.write(sb.toString().getBytes());
        } catch (IOException ex) {
            Logger.getLogger(XMLWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * write the video
     * @param video
     * @param label
     */
    public void write(Video video, JLabel label) {
        try {
            if (video != null) {
                StringBuilder sb = new StringBuilder("");
                sb.append("<video>\n");

                sb.append("<filepath>").append(video.getFilepath()).append("</filepath>\n");
                sb.append("<filename>").append(video.getFilename()).append("</filename>\n");
                sb.append("<width>").append(label.getWidth()).append("</width>\n");
                sb.append("<height>").append(label.getHeight()).append("</height>\n");
                sb.append("<originalWidth>").append(video.getWidth()).append("</originalWidth>\n");
                sb.append("<originalHeight>").append(video.getHeight()).append("</originalHeight>\n");
                sb.append("<positionx>").append(label.getLocation().x).append("</positionx>\n");
                sb.append("<positiony>").append(label.getLocation().y).append("</positiony>\n");

                sb.append("<actions>\n");
                for (int i = 0; i < video.getActions().size(); i++) {
                    Action action = video.getActions().get(i);
                    sb.append("<action>\n");
                    sb.append("<actionNum>").append(action.getActionNumber()).append("</actionNum>\n");
                    sb.append("<param1>").append(action.getParam1()).append("</param1>\n");
                    sb.append("<param2>").append(action.getParam2()).append("</param2>\n");
                    sb.append("</action>\n");
                }
                sb.append("</actions>\n");

                sb.append("</video>\n");
                fos.write(sb.toString().getBytes());
            }
        } catch (IOException ex) {
            Logger.getLogger(XMLWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * write image
     * @param image
     * @param label
     */
    public void write(Image image, JLabel label) {
        try {
            if (image != null) {
                StringBuilder sb = new StringBuilder("");
                sb.append("<image>\n");

                sb.append("<filepath>").append(image.getFilepath()).append("</filepath>\n");
                sb.append("<filename>").append(image.getFilename()).append("</filename>\n");
                sb.append("<width>").append(label.getWidth()).append("</width>\n");
                sb.append("<height>").append(label.getHeight()).append("</height>\n");
                sb.append("<originalWidth>").append(image.getWidth()).append("</originalWidth>\n");
                sb.append("<originalHeight>").append(image.getHeight()).append("</originalHeight>\n");
                sb.append("<positionx>").append(label.getLocation().x).append("</positionx>\n");
                sb.append("<positiony>").append(label.getLocation().y).append("</positiony>\n");

                sb.append("<actions>\n");
                for (int i = 0; i < image.getActions().size(); i++) {
                    Action action = image.getActions().get(i);
                    sb.append("<action>\n");
                    sb.append("<actionNum>").append(action.getActionNumber()).append("</actionNum>\n");
                    sb.append("<param1>").append(action.getParam1()).append("</param1>\n");
                    sb.append("<param2>").append(action.getParam2()).append("</param2>\n");
                    sb.append("</action>\n");
                }
                sb.append("</actions>\n");
                sb.append("</image>\n");
                fos.write(sb.toString().getBytes());
            }
        } catch (IOException ex) {
            Logger.getLogger(XMLWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void write(Audio audio) {
        try {
            if (audio != null) {
                StringBuilder sb = new StringBuilder("");
                sb.append("<audio>\n");

                sb.append("<filename>").append(audio.getFilename()).append("</filename>\n");
                sb.append("<filepath>").append(audio.getFilepath()).append("</filepath>\n");

                sb.append("</audio>\n");
                fos.write(sb.toString().getBytes());
            }
        } catch (IOException ex) {
            Logger.getLogger(XMLWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void close() {
        try {
            StringBuilder sb = new StringBuilder("");
            sb.append("</MediaFile>\n");
            fos.write(sb.toString().getBytes());
            fos.close();
        } catch (IOException ex) {
            Logger.getLogger(XMLWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void write(Track track, JLabel label) {
        try {
            if (track != null) {
                StringBuilder sb = new StringBuilder("");
                sb.append("<track>\n");

                sb.append("<filename>").append(track.getName()).append("</filename>\n");
                sb.append("<width>").append(label.getWidth()).append("</width>\n");
                sb.append("<height>").append(label.getHeight()).append("</height>\n");

                sb.append("<actions>\n");
                for (int i = 0; i < track.getActions().size(); i++) {
                    Action action = track.getActions().get(i);
                    sb.append("<action>\n");
                    sb.append("<actionNum>").append(action.getActionNumber()).append("</actionNum>\n");
                    sb.append("<param1>").append(action.getParam1()).append("</param1>\n");
                    sb.append("<param2>").append(action.getParam2()).append("</param2>\n");
                    sb.append("</action>\n");
                }
                sb.append("</actions>\n");

                sb.append("<keyFrames>\n");
                for (int i = 0; i < track.getKeyframes().size(); i++) {
                    KeyFrame frame = track.getKeyframes().get(i);
                    sb.append("<keyframe>\n");
                    sb.append("<frameNum>").append(frame.getFrameNum()).append("</frameNum>\n");
                    sb.append("<x>").append(frame.getPosition_x()).append("</x>\n");
                    sb.append("<y>").append(frame.getPosition_y()).append("</y>\n");
                    sb.append("</keyframe>\n");
                }
                sb.append("</keyFrames>\n");

                sb.append("</track>\n");
                fos.write(sb.toString().getBytes());
            }
        } catch (IOException ex) {
            Logger.getLogger(XMLWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void writeSize(int CANVAS_WIDTH, int CANVAS_HEIGHT) {
        try {
            StringBuilder sb = new StringBuilder("");
            sb.append("<canvasWidth>");
            sb.append(CANVAS_WIDTH);
            sb.append("</canvasWidth>\n");
            sb.append("<canvasHeight>");
            sb.append(CANVAS_HEIGHT);
            sb.append("</canvasHeight>\n");
            fos.write(sb.toString().getBytes());
        } catch (IOException ex) {
            Logger.getLogger(XMLWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
